package cn.edu.njust.epms.dao;

import cn.edu.njust.epms.entity.HRRecord;
import cn.edu.njust.epms.common.R;
import cn.edu.njust.epms.entity.UserDetails;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

@Mapper
public interface HRDao {
    /**
     * 加入健康上报信息
     * @param record
     * @return
     */
    @Insert("insert into hr_record (uTemp,rTime,pid) values(#{uTemp},#{rTime},#{pid})")
    public boolean Add(HRRecord record);



    /**
     * 按条件查询
     * @param r
     * @return
     */
    @Select("<script>"+
            "SELECT * FROM hr_record inner join udetails on udetails.pid=hr_record.pid"+
            "<where>"+
            "   <if test='r.name != null and r.name.length > 0'>"+
            "       AND name like #{r.name}"+
            "   </if>"+
            "   <if test='r.Temp != null'>"+
            "       AND uTemp = #{r.temp}"+
            "   </if>"+
            "   <if test='r.uId != null'>"+
            "       AND udetails.uId = #{r.uId}"+
            "   </if>"+
            "   <if test='r.pid != null and r.pid.length > 0'>"+
            "       AND udetails.pid like #{r.pid}"+
            "   </if>"+
            "   <if test='r.date1 != null and r.date2 != null'>"+
            "       AND rTime between #{r.date1} and #{r.date2}"+
            "   </if>"+
            "</where>"+
            "</script>")
    @Results({
            @Result(column = "rId",property = "rId"),
            @Result(column = "rTime",property = "rTime"),
            @Result(column = "uTemp",property = "uTemp"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "pid",property = "ud.pid"),
            @Result(column = "name",property = "ud.name")
    })
    public List<HRRecord> selectByCondition(@Param("r") R r);

    @Delete("delete from hr_record where rId=#{rId}")
    public Boolean deleteHRRecord(HRRecord r);

    @Select("SELECT * FROM udetails WHERE pid not in (" +
            "SELECT pid FROM hr_record WHERE hr_record.rTime BETWEEN #{date1} AND #{date2})")
    public List<UserDetails> selectUdWithoutHR(Date date1,Date date2);

    @Select("SELECT * FROM udetails WHERE pid in (" +
            "SELECT pid FROM hr_record WHERE hr_record.rTime BETWEEN #{date1} AND #{date2})")
    public List<UserDetails> selectUdHR(Date date1,Date date2);
}
